package cn.itcast.wushangxiantu.Activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.BossAdapter;
import cn.itcast.wushangxiantu.UserData.ShangHaiPaiHang;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.UserData.UserBoss;
import cn.itcast.wushangxiantu.UserData.UserValue;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Boss extends AppCompatActivity {

    private TextView tvName;
    private TextView tvSm;
    private TextView tvFy;
    private ImageView iv;
    private ListView lv;
    private TextView tvNumber;
    private TextView tvGj;
    private TextView tvCt;
    private TextView tvBj;
    private TextView tvSh;
    private BossAdapter adapter;
    private TextView tvDaJia;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boss);
        initView();
    }

    private void initView() {
        tvName = (TextView) findViewById(R.id.tvName);
        tvSm = (TextView) findViewById(R.id.tvSm);
        tvFy = (TextView) findViewById(R.id.tvFy);
        iv = (ImageView) findViewById(R.id.iv);
        lv = (ListView) findViewById(R.id.lv);
        tvNumber = (TextView) findViewById(R.id.tvNumber);
        tvGj = (TextView) findViewById(R.id.tvGj);
        tvCt = (TextView) findViewById(R.id.tvCt);
        tvBj = (TextView) findViewById(R.id.tvBj);
        tvSh = (TextView) findViewById(R.id.tvSh);
        getData();
        getShangHaiData();
        tvDaJia = (TextView) findViewById(R.id.tvDaJia);
        tvDaJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              new Thread(){
                  @Override
                  public void run() {
                      try {
                          sleep(2000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      getDataDaJia();
                  }
              }.start();
            }
        });
    }

    private void getDataDaJia() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=daBoss", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("da1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("da2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                }catch (Exception e){
                    user=null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 3;
                handler.sendMessage(message);
            }
        });
    }

    private void getData() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=yulanBoss", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("yulanBoss1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("yulanBoss2", src);
                UserBoss user=null;
                try {
                    user = new Gson().fromJson(src, UserBoss.class);
                }catch (Exception e){
                    user=null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    private void getShangHaiData() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=shanghaipaihang", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("yulanBoss1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("yulanBoss2", src);
                ShangHaiPaiHang user =null;
                try {
                    user = new Gson().fromJson(src, ShangHaiPaiHang.class);
                }catch (Exception e){
                    user=null;
                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @SuppressLint("NewApi")
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    UserBoss userBoss = (UserBoss) message.obj;
                    if (null!=userBoss){
                        UserValue value = new UserValue(Login.getUser());
                        tvBj.setText("当前暴击:" + value.getBj());
                        tvGj.setText("当前攻击:" + value.getGj());
                        tvCt.setText("当前穿透:" + value.getCt());
                        tvNumber.setText("当前剩余次数:" + userBoss.getUserNumber());
                        tvSh.setText("我的输出:" + userBoss.getUserSh());
                        tvFy.setText("防御：" + userBoss.getBossFy());
                        tvSm.setText("生命：" + userBoss.getBossSm());
                        Log.v("1231313123",""+userBoss.getBossdj()%5);
                        switch (userBoss.getBossdj()%5){
                            case 0:
                                tvName.setText("LV:" + userBoss.getBossdj() + "灭世邪龙");
                                break;
                            case 1:
                                tvName.setText("LV:" + userBoss.getBossdj() + "灭世邪龙");
                                break;
                            case 2:
                                tvName.setText("LV:" + userBoss.getBossdj() + "灭世邪龙");
                                break;
                            case 3:
                                tvName.setText("LV:" + userBoss.getBossdj() + "灭世邪龙");
                                break;
                            case 4:
                                tvName.setText("LV:" + userBoss.getBossdj() + "灭世邪龙");
                                break;
                        }
                    }else{
                        ToastUtil.toast(Boss.this,"服务器坏掉了...");
                    }
                    break;
                case 2:
                    ShangHaiPaiHang shangHaiPaiHang = (ShangHaiPaiHang) message.obj;
                 if (null!=shangHaiPaiHang){
                     if (adapter == null) {
                         adapter = new BossAdapter(Boss.this);
                     }
                     adapter.setShangHaiPaiHang(shangHaiPaiHang);
                     lv.setAdapter(adapter);
                 }else{
                     ToastUtil.toast(Boss.this,"服务器坏掉了...");
                 }
                    break;
                case 3:
                    User user= (User) message.obj;
                    if (null!=user){
                        GeneralDialog("Boss详情",user.getStw());
                        getData();
                        getShangHaiData();
                    }else{
                        ToastUtil.toast(Boss.this,"服务器坏掉了...");
                    }
                    break;
            }
            return false;
        }
    });
    public void GeneralDialog(String head, String content) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(head)
                .setMessage(content)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }
}